﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;

namespace PresentationLayerWebForms
{
    public partial class ChooseRoomType : ReservationBaseController
    {
        private RoomTypeRepository roomTypeRepository = new RoomTypeRepository();
        protected Booking booking;

        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            currentPage = "ChooseRoomType.aspx";

            booking = (Booking)Session["Booking"];

            if (booking != null)
            {
                gvRoomType.DataSource = SearchAvailiableRoomType(booking);
                gvRoomType.DataBind();
                txtNoOfRoom.Value = booking.NoOfRoom.ToString();
                txtNoOfNights.Value = ((DateTime)booking.EndDate).Subtract((DateTime)booking.StartDate).TotalDays.ToString();

                decimal vAmt = 0;
                try
                {
                    if (booking.Payment != null)
                    {
                        vAmt = booking.Payment.Voucher.Amount;
                    }
                }
                catch (NullReferenceException execp)
                {
                    Console.Write(execp.Message);
                }
                txtVoucherAmt.Value = vAmt.ToString();
                HttpContext.Current.Items["voucherAmt"] = vAmt;
            }
            else
            {
                this.FirstPage();
            }
        }

        private List<RoomType> SearchAvailiableRoomType(Booking booking)
        {
            List<RoomType> availiableRoomType = new List<RoomType>();
            RoomTypeRepository roomTypeRepository = new RoomTypeRepository();

            IQueryable<RoomType> abc = roomTypeRepository.GetAll();
            return roomTypeRepository.GetAll().ToList();
        }

        protected void gvRoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow gridViewRow = gvRoomType.SelectedRow;
            int roomTypeID = int.Parse(gridViewRow.Cells[3].Text.ToString());

            Booking booking = (Booking)Session["Booking"];

            booking.RoomType = businessComponentsFacade.FindRoomType(roomTypeID);

            Session["Booking"] = booking;

            this.NextPage();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.LastPage();
        }

        
        protected string getPrice(string price)
        {

            return "ok";
        }
    }
}